package com.matt.docpro.service;

import com.matt.docpro.mapper.*;
import com.matt.docpro.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * AppointmentService
 * Created by on 2024/02/20.
 */
@Service
public class UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    DoctorMapper doctorMapper;
    @Autowired
    PatientMapper patientMapper;
    @Autowired
    CheckinMapper checkinMapper;

    public List<User> getAll() {
        return userMapper.getAll();
    }

    public User getById(String id) {
        return userMapper.getById(id);
    }

    public List<User> getOtherUsers(String id) {
        return userMapper.getOtherUsers(id);
    }

    public User getUser(String account, String password) {
        return userMapper.getUser(account, password);
    }

    public void insert(User user) {
        userMapper.insert(user);
        switch (user.getRole()) {
            case "1" -> {
                Doctor doctor = new Doctor();
                doctor.setId(user.getId());
                doctor.setIcon(user.getIcon());
                doctor.setMail(user.getMail());
                doctorMapper.insert(doctor);
            }
            case "2" -> {
                Patient patient = new Patient();
                patient.setId(user.getId());
                patient.setIcon(user.getIcon());
                patient.setMail(user.getMail());
                patientMapper.insert(patient);
            }
            case "3" -> {
                Checkin checkin = new Checkin();
                checkin.setId(user.getId());
                checkin.setIcon(user.getIcon());
                checkin.setMail(user.getMail());
                checkinMapper.insert(checkin);
            }
            default -> {
                Admin admin = new Admin();
                admin.setId(user.getId());
                admin.setIcon(user.getIcon());
                admin.setMail(user.getMail());
                adminMapper.insert(admin);
            }
        }
    }

    public void updatePassword(String account, String password) {
        userMapper.updatePassword(account, password);
    }

    public void update(User user) {
        userMapper.update(user);
        switch (user.getRole()) {
            case "1" -> userMapper.updateDoctorIcon(user);
            case "2" -> userMapper.updatePatientIcon(user);
            case "3" -> userMapper.updateCheckinIcon(user);
            default -> userMapper.updateAdminIcon(user);
        }
    }

    public void updateBaned(String id) {
        userMapper.updateBaned(id);
    }

    public void delete(User user) {
        userMapper.deleteById(user.getId());
        switch (user.getRole()) {
            case "1" -> doctorMapper.deleteById(user.getId());
            case "2" -> patientMapper.deleteById(user.getId());
            case "3" -> checkinMapper.deleteById(user.getId());
            default -> adminMapper.deleteById(user.getId());
        }
    }

}
